<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<script src="../../js/global.js"></script>
		<style type="text/css">
			body {
				background: white;
			}
			
			.list-part {
				margin-top: 12%;
			}
		</style>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">系统设置</h1>
		</header>
		<ul class="mui-table-view list-part">
			<li id="pushNotice" class="mui-table-view-cell">
				<!--<a class="mui-navigate-right" href="#">
					消息通知
				</a>-->
				推送通知
				<div class="mui-switch mui-active" id="mySwitch">
					<div class="mui-switch-handle"></div>
				</div>
			</li>
			<li class="mui-table-view-cell">
				<a class="mui-navigate-right">
					设置接口地址
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a class="mui-navigate-right">
					清除缓存
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a id="update" class="mui-navigate-right">
					检查更新
				</a>
			</li>
			<!--<li class="mui-table-view-cell"  m-href="experiments.html">
				<a class="mui-navigate-right" href="javaScript:;">
					实验室
				</a>
			</li>
			<li class="mui-table-view-cell" id="laboratory">
				<a class="mui-navigate-right">
					实验室
				</a>
			</li>-->
			<li class="mui-table-view-cell">
				<a id="exitLogin" class="mui-navigate-right">
					退出登录
				</a>
			</li>
		</ul>

		<script src="../../js/mui.min.js"></script>
		<script src="../../js/app.js"></script>
		<script src="../../js/jquery-3.2.1.js"></script>
		<script type="text/javascript">
			(function($, doc) {
				$.init();
				var settings = app.getSettings();
				window.addEventListener('show', function() {
					var state = app.getState();
				}, false);
				$.plusReady(function() {
					//初始化Push
					initPush();
					plus.screen.lockOrientation("portrait-primary");
				});
			}(mui, document));

			mui(".mui-table-view").on('tap', "#laboratory", function() {
				plus.screen.lockOrientation("portrait-primary");
				mui.openWindow({
					url: "experiments.html",
					id: "experiments",
					extras: { //自定义扩展参数，可以用来处理页面间传值
					},
					createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
				});
			});

			var exit = document.getElementById('exitLogin');
			exit.addEventListener('tap', function(event) {
				var btnArray = ['否', '是'];
				mui.confirm('是否退出登录', '确认？', btnArray, function(e) {
					if(e.index == 1) {
						exitLogin();
					} else {}
				})
			});

			function exitLogin() {
				var loginStatu = "0"; //为零退出登录
				plus.storage.setItem("loginStatu", loginStatu);
				console.log("test--" + plus.storage.getItem("loginStatu"));
				var wb = plus.webview.currentWebview();
				plus.webview.close(wb, 'slide-out-right');
				plus.webview.close('main', 'slide-out-right');
			}

			//检查更新
			document.getElementById('update').addEventListener('tap', function() {
				obtainnumber();
				setTimeout(function() {
					checked();
				}, 300)
			})

			function obtainnumber() {
				function plusReady() {
					plus.runtime.getProperty(plus.runtime.appid, function(inf) {
						wgtVer = inf.version;
					});
				}
				if(window.plus) {
					plusReady();
				} else {
					document.addEventListener('plusready', plusReady, false);
				}
			}

			var mVersionName;
			var mDescrition;
			var mCode;
			var mFileName;

			function checked() {
				mui.getJSON("../../manifest.json", null, function(data) {
					mCode = data.version.code;
				});

				mui.ajax(UPDATE_URL, {
					dataType: 'json', //服务器返回json格式数据
					type: 'get', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒
					headers: {
						'Content-Type': 'text/html'
					},
					success: function(data) {
						//console.log(JSON.stringify(data));
						var code = data.obj.data[0].version_code;
						mVersionName = data.obj.data[0].version_name;
						mDescrition = data.obj.data[0].desctition;
						mFileName = data.obj.data[0].file_name;
						console.log(code + ":" + mCode);
						if(parseInt(code) > parseInt(mCode)) {
							updateDIALOG();
						} else {
							mui.toast("目前已经是最新版本！");
						}
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						console.log(type);
						return callback('接口正在维护中');
					}
				});
			}

			function updateDIALOG() {
				console.log("执行下载更新");
				var btnArray = ['否', '是'];
				console.log(mDescrition);
				mui.confirm(mDescrition, '发现新版本:' + mVersionName, btnArray, function(e) {
					if(e.index == 1) {
						console.log("确认更新");
						downWgt();
					} else {
						console.log("取消更新");
					}
				})
			}

			function downWgt() {
				plus.nativeUI.showWaiting("下载wgt文件...");
				console.log(mDownloadUrl + mFileName); //
				plus.downloader.createDownload(mDownloadUrl + mFileName, {
					filename: "_doc/update/"
				}, function(d, status) {
					if(status == 200) {
						console.log("下载wgt成功：" + d.filename);
						installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			}

			function installWgt(path) {
				plus.nativeUI.showWaiting("安装wgt文件...");
				//path : _doc/update/20180110104106K96jD4KX(3).png
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			}
			//初始化Push
			function initPush() { // 在这里调用5+ API 
				if(mui.os.android) {
					var pushState = plus.storage.getItem("pushState");
					console.log("switch--pushState--" + pushState);
					if(pushState == "close") {
						document.getElementById('mySwitch').setAttribute("class", "mui-switch");
					} else {
						document.getElementById('mySwitch').setAttribute("class", "mui-switch mui-active");
					}
				} else {
					document.getElementById("pushNotice").remove();
				}
			}

			document.getElementById("mySwitch").addEventListener("toggle", function(event) {
				if(event.detail.isActive) {
					plus.storage.setItem("pushState", "open");
					pushEnable();
					console.log("你启动了开关");
				} else {
					pushDisable();
					plus.storage.setItem("pushState", "close");
					console.log("你关闭了开关");
				}
			})
		</script>
	</body>

</html>